""""
Case Type   : D库常用语法兼容-proc
Case Name   : B库支持procedure创建存储过程，不支持proc修改rename
Create At   : 2025-11-19
Owner       : chen
Description :
    1.创建proc
    2.创建procedure
    3.rename procedure
    4.rename proc
    5.清理环境
Expect      :
    1.合理报错
    2.成功
    3.成功
    4.合理报错
    5.成功
History     :
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node

log = Logger()
primary_sh = CommonSH('PrimaryDbUser')

class DDL_Proc_Case0018(unittest.TestCase):
    def setUp(self):
        log.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.dbname_b = Node('PrimaryDbBmode').db_name
        self.proc_name = 'p_proc_case018'
        self.new_proc_name = 'p_proc_new_case018'

    def test_proc(self):
        text = '-----step1: 创建proc; expect: 成功-----'
        log.info(text)
        msg = primary_sh.check_connection_status(self.constant.START_STATUS_MSG)
        self.assertTrue(msg)
        create_tb = f"""create proc {self.proc_name}()
            is
            begin
            RAISE INFO 'call procedure: {self.proc_name}';
            end;"""
        res = primary_sh.execut_db_sql(create_tb, dbname=self.dbname_b)
        log.info(res)
        self.assertIn('ERROR:  syntax error', res)

        text = '-----step2: 创建procedure; expect: 成功-----'
        log.info(text)
        create_tb = f"""create procedure {self.proc_name}()
            is
            begin
            RAISE INFO 'call procedure: {self.proc_name}';
            end;"""
        res = primary_sh.execut_db_sql(create_tb, dbname=self.dbname_b)
        log.info(res)
        self.assertIn(self.constant.CREATE_PROCEDURE_SUCCESS_MSG, res)
        detail_sql = f'\df {self.proc_name}();'
        res = primary_sh.execut_db_sql(detail_sql, dbname=self.dbname_b)
        log.info(res)
        detail_list = ['public', self.proc_name, 'void', 'normal', 'f', 'p']
        for detail in detail_list:
            self.assertIn(detail, res)
        find_sql = f"select provolatile from pg_proc where proname =" \
            f" '{self.proc_name}';"
        res = primary_sh.execut_db_sql(find_sql, dbname=self.dbname_b)
        log.info(res)
        self.assertIn('v', res)

        text = '-----step3: rename procedure; expect: 成功-----'
        log.info(text)
        alter_sql = f"alter procedure {self.proc_name}() rename to " \
            f"{self.new_proc_name};"
        res = primary_sh.execut_db_sql(alter_sql, dbname=self.dbname_b)
        log.info(res)
        self.assertIn(self.constant.ALTER_FUNCTION_SUCCESS_MSG, res)
        detail_sql = f'\df {self.new_proc_name}();'
        res = primary_sh.execut_db_sql(detail_sql, dbname=self.dbname_b)
        log.info(res)
        detail_list = ['public', self.new_proc_name, 'void', 'normal', 'f', 'p']
        for detail in detail_list:
            self.assertIn(detail, res)
        alter_sql = f"alter procedure {self.new_proc_name}() rename to " \
            f"{self.proc_name};"
        res = primary_sh.execut_db_sql(alter_sql, dbname=self.dbname_b)
        log.info(res)
        self.assertIn(self.constant.ALTER_FUNCTION_SUCCESS_MSG, res)

        text = '-----step4: rename proc; expect: 合理报错-----'
        log.info(text)
        alter_sql = f"alter proc {self.new_proc_name}() rename to " \
            f"{self.proc_name};"
        res = primary_sh.execut_db_sql(alter_sql, dbname=self.dbname_b)
        log.info(res)
        self.assertIn('ERROR:  syntax error', res)

    def tearDown(self):
        text = '-----step5: 清理环境; expect: 成功-----'
        log.info(text)
        drop_proc = f'''drop procedure if exists {self.proc_name}();'''
        res = primary_sh.execut_db_sql(drop_proc, dbname=self.dbname_b)
        log.info(res)
        self.assertIn(self.constant.DROP_PROCEDURE_SUCCESS_MSG, res)